package com.gouqi.mall.controller;

import com.gouqi.mall.domain.Pager;
import com.gouqi.mall.domain.Person;
import com.gouqi.mall.service.PersonService;
import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Component
@Api(basePath = "/api/v1", value = "人员接口", description = "测试用的人员接口")
@RestController
@RequestMapping(value = "/person")
public class PersonController {

    @Autowired
    @Qualifier("personServiceImpl")
    private PersonService personService;

    @ResponseBody
    @RequestMapping(value = "/getAllPersons",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "获取所有人员",notes = "获取所有人员信息，返回创建状态",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String")
    })
    public String getAllPersons(@RequestHeader(value = "token") String token, HttpServletRequest request) throws UnsupportedEncodingException {
        JSONObject jsonObject = new JSONObject();
        //验证token
        Boolean result = (Boolean) request.getAttribute("result");
        System.out.println("拦截token的结果："+result);
        if (result) {
            //返回json信息
            List<Person> list = personService.getAllPersons();
            jsonObject.put("result","success");
            jsonObject.put("data",list);
            jsonObject.put("message","成功");
            jsonObject.put("pager",new Pager(1,list.size(),10));
            jsonObject.put("status",200);
        } else {
            jsonObject.put("data","");
            jsonObject.put("pager",new Pager(1,0,10));
            jsonObject.put("result","failed");
            jsonObject.put("status",206);
            jsonObject.put("message","签名信息过期，请重新登录");
        }
        return jsonObject.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/getOnePersons/{name}",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "根据用户名获取人员",notes = "人员接口，据用户名获取人员，返回人员列表",httpMethod = "POST")
    public String getOnePersons(@ApiParam(required = true,name = "name",value = "姓名") @PathVariable(value = "name") String name){
        JSONObject jsonObject = new JSONObject();
        List list = personService.getOnePersonByName(name);
        System.out.println(list);
        jsonObject.put("result","success");
        jsonObject.put("data",list);
        jsonObject.put("message","成功···");
        jsonObject.put("pager",new Pager(1,list.size(),10));
        return jsonObject.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/getPagePerson/{name}/{page}",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "根据分页显示人员",notes = "人员接口，根据页数返回当前页人员信息",httpMethod = "POST")
    public String getOnePersons(
            @ApiParam(required = true,name = "name",value = "姓名") @PathVariable(value = "name") String name,
            @ApiParam(required = true,name = "page",value = "页码") @PathVariable(value = "page") Integer page
    ){
        JSONObject jsonObject = new JSONObject();

        List list = personService.getPagePersons(name,page);
        System.out.println(list);
        jsonObject.put("result","success");
        jsonObject.put("data",list);
        jsonObject.put("message","成功···");
        jsonObject.put("pager",new Pager(1,list.size(),10));
        return jsonObject.toString();
    }
}
